package utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/*
 * 
 * This Class is used for send sms with the help of getHelper class
 */
public class SendSMSHelper {

	static long TimeStamp;
	static String md5Part;
	static String codestr;
	static String result;
	static String url;

	public static boolean send(String msg, String mobileNum) {

		try {
			codestr = URLEncoder.encode(msg, "GBK");

			TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

			md5Part = MD5Helper.getMD5Str("123456_" + TimeStamp + "_topsky");

			url = "http://115.182.33.133/houtai/sms.php?" + "cpid=4617&"
					+ "password=" + md5Part + "&channelid=3028&" + "tele="
					+ mobileNum + "&" + "msg=" + codestr + "&" + "timestamp="
					+ TimeStamp;

			result = GetHelper.doHttpGet(url);
			System.out.println("sms result: " + result);
			// result="success:322232";
			if (String.valueOf(result.charAt(0)).equals("s"))
				return true;
			else
				return false;

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}

	}

	/*
	 * return balance value of SMS platform ���ض���ƽ̨���
	 */
	public static float getBalance() {

		TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

		md5Part = MD5Helper.getMD5Str("123456_" + TimeStamp + "_topsky");

		url = "http://115.182.33.133/houtai/sms_ye.php?" + "userid=4617&"
				+ "password=" + md5Part + "&channelid=3028&" + "timestamp="
				+ TimeStamp;

		result = GetHelper.doHttpGet(url);
		System.out.println("sms result: " + result);
		// result="success:322232";
		if (String.valueOf(result.charAt(0)).equals("s")) {
			// System.out.println(Float.parseFloat(
			// result.substring(result.indexOf(":")+1)));
			return Float.parseFloat(result.substring(result.indexOf(":") + 1));
		} else {
			System.out.println("Error");
			return -1;
		}

	}

	public static boolean newSend(String msg, String mobileNum) {
		try {
			codestr = URLEncoder.encode(msg, "GBK");

			TimeStamp = System.currentTimeMillis() / 1000; // the unit is second

			md5Part = MD5Helper.getMD5Str("youbox123456_" + TimeStamp + "_topsky");

			url = "http://115.182.33.133/houtai/sms.php?" + "cpid=6956&"
					+ "password=" + md5Part + "&channelid=3028&" + "tele="
					+ mobileNum + "&" + "msg=" + codestr + "&" + "timestamp="
					+ TimeStamp;

			result = GetHelper.doHttpGet(url);
			System.out.println("sms result: " + result);
			// result="success:322232";
			if (String.valueOf(result.charAt(0)).equals("s"))
				return true;
			else
				return false;

		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
//		boolean b = false;
//		try {
//			codestr = URLEncoder.encode(msg+"【友宝电子】", "GB2312");
//			url = "http://183.250.110.222:9999/smsGBK.aspx?" + "action=send&"
//					+ "userid=227&" + "account=youbox&" + "password=youbaoit&"
//					+ "mobile=" + mobileNum + "&content=" + codestr
//					+ "&sendTime=&extno=";
//			result = GetHelper.doHttpGet(url);
//			System.out.println("sms result: " + result);
//			if (result.contains("Success")) {
//				b = true;
//			}
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
//		return b;
	}

	public static void main(String[] args) {
		// send("你好", "18960877052");
		// getBalance();
		newSend("您的中通快件,桂苑11,取件号:15.在17:20前领取.单号:889707986978 服务电话:18650346254",
				"18650450228");
//		System.out.println(new Timestamp(new Date().getTime()));
	}

}
